package com.shenzw.mybatisdemo.controller.request;

import cn.hutool.core.text.CharSequenceUtil;
import com.shenzw.mybatisdemo.aop.CheckParam;
import com.shenzw.mybatisdemo.aop.ConvertPathToBase64;
import com.shenzw.mybatisdemo.controller.param.BaseGroup;
import lombok.Data;
import org.hibernate.validator.constraints.Range;

import javax.validation.constraints.*;

/**
 * 演示校验用户请求参数
 *
 * @author shenzw
 * @date 2023/12/7
 */
@Data
public class UserRequest {

    @NotNull(groups = BaseGroup.Update.class, message = "用户id不能为空")
    private Integer id;

    @NotNull(message = "用户名不能为空")
    @Size(min = 6, max = 20, message = "用户名长度必须在 6～20 字符之间")
    private String name;

    @NotBlank(message = "手机号不能为空")
    @Pattern(regexp = "1[3-9]\\d{9}", message = "手机号格式不正确")
    @Size(max = 11, min = 11, message = "手机号长度为 11")
    private String phone;

    @Email(message = "邮箱格式不正确")
    private String email;

    @Range(min = 1, max = 120, message = "年龄只能在 1～120 之间")
    private Integer age;

    @CheckParam(paramAllowValues = {"man", "woman"}, message = "性别只能传入 man 和 woman")
    private String sex;

    private String idCard;

    private String realName;

    /**
     * 照片地址，需要对地址进行统一转化，转为 Base64形式
     * 可以通过自定义注解 + ControllerAdvice + RequestBodyAdvice 实现对请求参数的转换
     */
    @ConvertPathToBase64
    private String phoneImage;

    /**
     * 证件号码和姓名不能同时为空
     */
    public void checkIdCardAndRealName() {
        if (CharSequenceUtil.isAllBlank(this.idCard, this.realName)) {
            throw new RuntimeException("身份证号和姓名不能同时为空");
        }
    }
}
